<template>
  <div class="wholes">

    <template v-if="state == 1">
      <div class="title">新增合同信息</div>
    </template>
    <template v-if="state == 0">
      <div class="title">修改合同信息</div>
    </template>    

    <el-form ref="form" :model="form" label-width="150px">
      <el-form-item label="客户名称">
        <el-input v-model="form.customer_Name"></el-input>
        <template v-if="state =='1'">
          <el-button type="danger" class="btn" @click="back">重选</el-button>
        </template>
      </el-form-item>

      <el-form-item label="合同编号" class="form-1">
        <template v-if="state =='1'">
            <el-input v-model="form.contract_no" ></el-input>
          <i class="el-icon-question"></i>
          <span>自动编号</span>
        </template>
        <template v-else>
          <el-input v-model="form.contract_no" disabled></el-input>
          <i class="el-icon-question"></i>
          <span>不可修改</span>
        </template>
      </el-form-item>

      <el-form-item label="合同分类" required>
        <el-select v-model="form.contract_part" placeholder="请选择">
          <el-option v-for="(item,index) in kinds" :key="index" :label="item.kind" :value="item.value"></el-option>
        </el-select>
        <el-button type="success" class="btn" @click="add"
          ><i class="el-icon-menu"></i>新增</el-button
        >
      </el-form-item>

      <el-form-item label="订单编号">
        <el-input v-model="form.order_id"></el-input>
        <el-button type="info" class="btn">...</el-button>
      </el-form-item>

      <el-form-item label="起始日期" required>
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.contract_startday"
            style="width: 200px"
          ></el-date-picker>
        </el-col>
      </el-form-item>

      <el-form-item label="录入时间" required>
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.contract_enterday"
            style="width: 200px"
          ></el-date-picker>
        </el-col>
      </el-form-item>

      <el-form-item label="到期日期" required>
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.contract_endday"
            style="width: 200px"
          ></el-date-picker>
        </el-col>
      </el-form-item>

      <el-form-item label="总金额" required>
        <el-input v-model="form.contract_summoney"></el-input>
        <span>元</span>
      </el-form-item>

      <el-form-item label="已收款" required>
        <el-input v-model="form.contract_receivemoney"></el-input>
        <span>元</span>
        <template v-if="state == '0'">
          <el-button type="success" class="btn" @click="getmoney"
          ><i class="el-icon-menu"></i>新增</el-button
        >
        </template>
      </el-form-item>

      <el-form-item label="是否提供发票">
        <el-select v-model="form.contract_tax" placeholder="请选择">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否含税">
        <el-select v-model="form.contract_invoice" placeholder="请选择">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="附件" class="form-1">
        <el-upload
         class="upload-demo"
          action="http://192.168.221.217:8080//document/addDocument"
          name="fileList"
          :on-success="uploadSuccess"
          :on-error="uploadFail"
        >
          <el-button size="small" type="primary">选择文件</el-button>
        </el-upload>
      </el-form-item>

      <el-form-item label="业务员">
        <el-select v-model="form.employee_id" placeholder="请选择">
          <el-option label="超级管理员" value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注" class="form-1">
        <el-input
          type="textarea"
          v-model="form.contract_comment"
          class="remark"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="onSubmit">保存</el-button>
        <el-button type="info" @click="backList">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { timeFormat } from "../../../utils/utils";
import { addcontranct } from '../../../http/apis/agreementApi/index'
import { changecontranct } from '../../../http/apis/agreementApi'
export default {
  data() {
    return {
      state:'',
      timePoint: 0,
      form: {
        customer_Name: "",//客户名称
        contract_no: "",//合同编号
        contract_part: "",// 合同分类
        order_id: "",//订单编号
        contract_startday: "",//起始日期
        contract_endday: "",//到期日期
        contract_summoney: "",//总金额
        contract_receivemoney: "",//已收款
        contract_tax: "",//是否提供发票批
        contract_invoice: "",//是否含税
        contract_comment: "",//备注
        contract_enterday:'',//录入数件
        employee_id:'',//业务员
        contract_debtmoney: 0,
        customer_Id: "",//客户编号
        
      },
       fileList: [],
       kinds:[
         {value:1,kind:'买卖'},
         {value:2,kind:'租赁'},
         {value:3,kind:'工程'},
         {value:4,kind:'技术'},
         {value:5,kind:'委托'},
         {value:6,kind:'其他'},
         ],
         input1:'',
         input2:''
    };
  },

  mounted() {
    this.state = this.$route.query.state
    console.log(this.state);
    this.timePoint = Date.now();
    this.form.contract_no = this.timePoint;
    // console.log(this.$route.query);
    this.form.customer_Name = this.$route.query.customer_name;
    this.form.contract_startday = timeFormat(this.timePoint);
    this.form.contract_enterday = timeFormat(this.timePoint);
    this.form.customer_Id = this.$route.query.customer_Id
    // console.log(this.$route.query.customer_Id);
    if( this.state == '0' ){
      this.form = this.$route.query
      this.form.customer_Id = this.$route.query.customer_Id
      console.log(this.form);
    }
  },

  methods: {
    onSubmit() {
        if(this.state === "1"){
          this.form.customer_Id = this.$route.query.customer_Id
          this.form.contract_startday = timeFormat(this.timePoint);
          this.form.contract_enterday = timeFormat(this.timePoint);
          this.form.contract_endday = timeFormat(this.form.contract_endday)
          this.form.contract_invoice = Number(this.form.contract_invoice)
          this.form.contract_part = Number(this.form.contract_part)
          this.form.customer_Id = Number(this.form.customer_Id)
          this.form.contract_receivemoney = Number(this.form.contract_receivemoney)
          this.form.contract_summoney = Number(this.form.contract_summoney)
          this.form.contract_no= String(this.form.contract_no)
          this.form.employee_id= Number(this.form.employee_id)

          console.log(this.form);
          addcontranct(this.form)
          .then((contract_id)=>{
            console.log('请求成功');
            console.log(contract_id);


          })
          .catch((err)=>{
            console.log('请求失败',err);
          })
          console.log("submit");
          this.$router.push("/agreement/agreementNext");
        }else if(this.state === "0"){
          this.form = this.$route.query
          console.log(this.form.employee_id);
          this.form.contract_startday = timeFormat(this.form.contract_startday);
          this.form.contract_enterday = timeFormat(this.form.contract_enterday);
          this.form.contract_endday = timeFormat(this.form.contract_endday)
          this.form.order_id = Number(this.form.order_id)
          this.form.customer_Id = Number(this.form.customer_Id)
          this.form.contract_summoney = Number(this.form.contract_summoney)
          this.form.contract_receivemoney = Number(this.form.contract_receivemoney)
          this.form.contract_debtmoney = Number(this.form.contract_debtmoney)
          if( this.form.employee_id == '0'|| this.form.employee_id == '超级管理员' ){
            this.form.employee_id = 0
          }else{
            this.form.employee_id =  1
          }
          if(this.form.contract_tax == '是'){
            this.form.contract_tax = "1"
          }else if(this.form.contract_tax == '否'){
            this.form.contract_tax = "0"
          }
          if(this.form.contract_invoice == '是'){
            this.form.contract_invoice = 1
          }else if(this.form.contract_invoice == '否'){
            this.form.contract_invoice = 0
          }
          if(this.form.contract_part == '1'){
            this.form.contract_part = 1
          }else if(this.form.contract_part == '2'){
              this.form.contract_part = 2
          }else if(this.form.contract_part == '3'){
              this.form.contract_part = 3
          }else if(this.form.contract_part == '4'){
              this.form.contract_part = 4
          }else if(this.form.contract_part == '5'){
              this.formcontract_part = 5
          }else {
              this.form.contract_part = 6
            }
          console.log(this.form);
          changecontranct(this.form)
          .then((res)=>{
            console.log('请求成功',res);
          })
          .catch((err)=>{
            console.log('请求失败',err);
          })
          this.$router.push("/agreement/agreementNext");
        }
    },
    back() {
      this.$router.back();
    },

    backList(){
      this.$router.push({path:'/agreement/agreementNext'})
    },

    add(){
      this.$router.push({path:'/agreement/addkinds',query:this.kinds})
    },

    getmoney(){
          this.$alert(
            '<div class="div1"><span>到款金额</span> <input class="input1" id="input1"v-model="input1"><span>元</span></div> <div class="div2"><span>备注</span><textarea class="input2" id="input2">',
            "新增到款",
            {
              dangerouslyUseHTMLString: true,
            }
          ).then(() => {
            this.input1 = document.getElementById("input1").value;
            // console.log(this.input1);
            this.input2 = document.getElementById("input2").value;
            // console.log(this.input2);
            this.form.contract_receivemoney =  Number(this.input1) + Number(this.form.contract_receivemoney)

            document.getElementById("input1").value = 0;
            document.getElementById("input2").value = "";
          });
    },

    // 上传成功的回调函数
    uploadSuccess(res) {
      console.log('成功：', res);
      // this.fileList.push(res.data.fileList[0]);
    },

    // 上传失败的回调函数
    uploadFail(err) {
      console.log('失败：', err);
    },



  },
};
</script>

<style lang="scss" scoped>
.title{
  font-size: 18px;
  font-weight: 600;
  text-indent: 2em;
  margin-top: 10px;
}
form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 1100px;
  margin-top: 20px;
}
.el-form-item {
  width: 500px;
}
.form-1 {
  width: 1000px;
}
.el-input {
  width: 150px;
}
.btn {
  margin-left: 10px;
  box-sizing: border-box;
}
.remark {
  width: 1000px;
}
.el-col-11{
  width: 80%;
}
.el-col {
   overflow: initial;
}
</style>